{{ define "body" }}
    <img src="data:{{ .mime }};base64,{{ .base64 }}" id="image-data" onclick="download()" style="width:100%" />
{{ end }}

{{ define "js" }}
<script type="application/javascript">
    function download() {
        {{ if eq .style "basicui" }}
            var a = document.createElement('a');

            a.href = document.getElementById('image-data').src;
            a.download = '{{ .filename }}';
            a.click();
        {{ else }}
            var matches = document.getElementById('image-data').src.match(/^data:(.+\/.+);base64,(.*)$/);

            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("POST", "{{ .Request.URL.String }}", true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.send("mime=" + encodeURIComponent(matches[1]) + "&payload=" + encodeURIComponent(matches[2]));
        {{ end }}
    }

    {{ if gt .refresh 0 }}
        setTimeout(function(){
            window.location.reload(true);
        }, {{ .refresh }});
    {{ end }}
</script>
{{ end }}